package com.artisan.domain.service;

import com.artisan.domain.dto.PageDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.dto.VersionDto;
import com.artisan.domain.entity.MStartPage;
import com.artisan.domain.entity.MVersion;
import com.artisan.domain.repositoryInterface.IVersionRepository;
import com.artisan.domain.serviceInterface.IVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 版本业务类
 */
@Service
public class VersionService implements IVersionService {
    @Autowired
    private IVersionRepository versionRepository;
    @Override
    public PageDto<VersionDto> findList(int platform, String number, int pageIndex, int pageSize) {
        Pageable pageable = new QPageRequest(pageIndex, pageSize);
        Page<MVersion> page = this.versionRepository.findList(platform, number, pageable);

        List<VersionDto> dtos = new ArrayList<>();
        page.getContent().forEach(model -> {
            VersionDto dto = new VersionDto();
            dto.setId(model.getId());
            dto.setNumber(model.getNumber());
            dto.setPlatform(model.getPlatform());
            dto.setUpdateType(model.getUpdateType());
            dto.setAddDate(model.getAddDate());
            dto.setStatus(model.getStatus());
            dto.setUrl(model.getUrl());
            dto.setContent(model.getContent());

            dtos.add(dto);
        });

        PageDto<VersionDto> result = new PageDto();
        result.setContent(dtos);
        result.setNumber(page.getNumber());
        result.setSize(page.getSize());
        result.setTotalElements(page.getTotalElements());

        return result;
    }

    @Override
    public VersionDto find(int id) {
        MVersion model = this.versionRepository.findById(id).get();

        VersionDto dto = new VersionDto();
        dto.setId(model.getId());
        dto.setNumber(model.getNumber());
        dto.setPlatform(model.getPlatform());
        dto.setUpdateType(model.getUpdateType());
        dto.setAddDate(model.getAddDate());
        dto.setStatus(model.getStatus());
        dto.setUrl(model.getUrl());
        dto.setContent(model.getContent());

        return dto;
    }

    @Override
    public boolean delete(int id) {
        this.versionRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean update(VersionDto dto) {
        return false;
    }
}
